package jinse.spider;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import jinse.entity.Jinse;
import jinse.entity.JinseContent;
import jodd.http.HttpRequest;

public class SpiderJinseForApi {
	private final String url = "http://www.jinse.com/ajax/lives/getList?search=&id=13839&flag=down";

	public Jinse spider() {
		String json = HttpRequest.get(url).send().bodyText();
		JSONObject JsonObj = JSONObject.parseObject(json);
		
		JSONObject jsonObject = JsonObj.getJSONObject("data");
		Set<String> keySet = jsonObject.keySet();
		List<JinseContent> dateObjs = new ArrayList<JinseContent>();
		for (String theDate : keySet) {
			String dateJson = jsonObject.getString(theDate);
			JSONArray parseArray = JSONObject.parseArray(dateJson);
			for (int i = 0; i < parseArray.size(); i++) {
				dateObjs.add(parseArray.getObject(i, JinseContent.class));
			}
		}
		return new Jinse(JsonObj.getLongValue("code"),dateObjs);
	}

	public static void main(String[] args) {
		System.out.println(new SpiderJinseForApi().spider());
	}
}
